package com.example.daytest.FileHandle;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.UUID;

/**
 * @Auther: DWHey95
 * @company 亚信科技
 * @Date: CREATE 2021/5/10 22:00
 * @Description: 文件读取以及处理
 */
public class FileHandle {


    /**
     * 插入uuid
     * @throws Exception
     */

    public static void  readTxt() throws Exception {
        File file = new File("C:/Users/Administrator\\Desktop\\suit_pkg.sql");
        InputStreamReader read = new InputStreamReader(new FileInputStream(file), "utf-8");//考虑到编码格式
        BufferedReader bu = new BufferedReader(read);

        String lineText = null;

        //3.边读边写
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(new File("C:/Users/Administrator\\Desktop\\suit_pkg1.sql")));
        BufferedWriter wr = new BufferedWriter(out);

        while ((lineText = bu.readLine()) != null ) {
            wr.write("("+ UUID.randomUUID().toString().replaceAll("-", "") +","+ lineText+ "\r\n");
            //wr.write("("+ lineText+ "\r\n");
           // wr.write("INSERT INTO td_suit_channel(id, suit_id, channel_id, channel_name,state, create_date, create_by, ext1)VALUES"+lineText+"\r\n");

        }
        read.close();
        wr.close();

        System.out.println(System.currentTimeMillis());


    }




    public void test2() throws Exception {
        //1.读取文件
        File file = new File("C:/Users/Administrator\\Desktop\\1.txt");
        InputStreamReader read = new InputStreamReader(new FileInputStream(file), "utf-8");//考虑到编码格式
        BufferedReader bu = new BufferedReader(read);



        //2.拼接字符串
        String lineText = null;


        //3.边读边写
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(new File("C:/Users/Administrator\\Desktop\\写出文件.txt")));
        BufferedWriter wr = new BufferedWriter(out);
        int count=0;
        while ((lineText = bu.readLine()) != null ) {

            wr.write(lineText+"\r\n");
            count++;
            if (count % 5000 == 0){
                wr.write("insert all\n" +
                        "select * from dual;");
            }

        }
        read.close();
        wr.close();

        System.out.println(System.currentTimeMillis());
        System.out.println("333333" + System.currentTimeMillis());


    }



    public void readTxt3() throws IOException {
        int count = 1;
        FileInputStream inputFile = new FileInputStream("C:/Users/Administrator\\Desktop\\1.txt");
        String line = ""; // 用来保存每行读取的内容
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputFile));
        ArrayList<String> list = new ArrayList<String>();
        line = reader.readLine(); // 读取第一行
        while (line != null) {
            list.add(line); // 将读到的内容添加到 集合当中。

            if (count % 50000 == 0) { //每一千行创建一个txt文件
                //FileOutputStream fos = new FileOutputStream("F:/20111001/"+count+".txt");
                PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream("C:/Users/Administrator\\Desktop\\" + count + ".txt")));
                for (Iterator<String> it = list.iterator(); it.hasNext(); ) {
                    pw.println(it.next().toString());
                }
                pw.flush();
                pw.close();
                list.clear();
            }
            line = reader.readLine(); // 读取下一行
            count++;
        }
        //将最后多的几行写完
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream("C:/Users/Administrator\\Desktop\\end.txt")));
        for (Iterator<String> it = list.iterator(); it.hasNext(); ) {
            pw.println(it.next().toString());
        }
        pw.flush();
        pw.close();
        list.clear();
        reader.close();
    }

    public void test3() throws Exception {
        System.out.println("222222" + System.currentTimeMillis());
        //1.读取文件
        File file = new File("C:\\Users\\Administrator\\Desktop\\号卡数据割接\\号卡数据割接\\割接\\校园白名单1.txt");
        InputStreamReader read = new InputStreamReader(new FileInputStream(file), "utf-8");//考虑到编码格式
        BufferedReader bu = new BufferedReader(read);

        File file2 = new File("C:\\Users\\Administrator\\Desktop\\号卡数据割接\\号卡数据割接\\割接\\校园白名单2.txt");
        InputStreamReader read2 = new InputStreamReader(new FileInputStream(file2), "utf-8");//考虑到编码格式
        BufferedReader bu2 = new BufferedReader(read2);

        File file3 = new File("C:\\Users\\Administrator\\Desktop\\号卡数据割接\\号卡数据割接\\割接\\校园白名单3.txt");
        InputStreamReader read3 = new InputStreamReader(new FileInputStream(file3), "utf-8");//考虑到编码格式
        BufferedReader bu3 = new BufferedReader(read3);

        //2.拼接字符串
        String lineText = null;
        //2.拼接字符串
        String lineText2 = null;
        //2.拼接字符串
        String lineText3 = null;
        String insert = "INSERT INTO td_school_whitelist(card_num, name, school_name) VALUES ('";
        String insert2 = "');";
        //3.边读边写
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\号卡数据割接\\号卡数据割接\\割接\\号卡校园白名单.txt")));
        BufferedWriter wr = new BufferedWriter(out);

        while ((lineText = bu.readLine()) != null && (lineText2 = bu2.readLine()) != null && (lineText3 = bu3.readLine()) != null) {
            System.out.println(insert + lineText + insert2);
            wr.write(insert + lineText + "','" + lineText2 + "','" + lineText3 + insert2 + "\r\n");

        }
        read.close();
        wr.close();

        System.out.println(System.currentTimeMillis());
        System.out.println("333333" + System.currentTimeMillis());


    }

    public static void main(String[] args) throws Exception {
            readTxt();

    }
}